struct Solution {}

impl Solution {
    pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
        let mut a = [0; 31];
        for i in 0..nums.len() {
            let mut n = nums[i];
            for i in 0..31 {
                if n % 2 == 1 { a[i] += 1 }
                n = n >> 1;
                if n == 0 { break; }
            }
        }
        let len = nums.len() as i32;
        a.iter().map(|n: &i32| (len - *n) * *n).sum()
    }
}

#[cfg(test)]
mod tests {
    use crate::Solution;

    #[test]
    fn it_works() {
        let cases = vec![
            (vec![4, 14, 2], 6)
        ];
        for (input, expected) in cases {
            assert_eq!(Solution::total_hamming_distance(input), expected);
        }
    }
}
